<template>
  <el-dialog
    :visible.sync="visible"
    :title="title"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataFrom"
      :model="dataFrom"
      label-width="100px"
      class="demo-form-inline"
      :inline="true"
    >
      <el-form-item prop="organName" label="模板名称" style="display: block;">
        <el-input
          v-model="dataFrom.organName"
          style="width: 460px;"
          placeholder="请输入邮件模板名称"
        />
      </el-form-item>
      <el-form-item label="客户名称">
        <el-input v-model="dataFrom.customerName" type="number"></el-input>
      </el-form-item>
      <el-form-item label="产品名称">
        <el-input v-model="dataFrom.productName" type="number"></el-input>
      </el-form-item>
      <el-form-item label="净值日期">
        <el-input v-model="dataFrom.valueDate" type="number"></el-input>
      </el-form-item>
      <el-form-item label="资产净值">
        <el-input v-model="dataFrom.valueAssets" type="number"></el-input>
      </el-form-item>
      <el-form-item label="资产份额">
        <el-input v-model="dataFrom.assetsShare" type="number"></el-input>
      </el-form-item>
      <el-form-item label="单位净值">
        <el-input v-model="dataFrom.valueUnit" type="number"></el-input>
      </el-form-item>
      <el-form-item label="累计净值">
        <el-input v-model="dataFrom.valueCombined" type="number"></el-input>
      </el-form-item>
      <el-form-item label="虚拟业绩报酬">
        <el-input v-model="dataFrom.virtualResults" type="number"></el-input>
      </el-form-item>
      <el-form-item label="虚拟净值">
        <el-input v-model="dataFrom.valueVirtual" type="number"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="addSubmit()">提交</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addOrUpdateOrgan } from '@/api/setting'
import { findInvestOrganByOrganId } from '@/api/setting'
export default {
  data() {
    return {
      visible: false,
      title: '新增邮件模板',
      dataFrom: {
        id: '',
        organName: '',
        customerName: '',
        productName: '',
        valueDate: '',
        valueAssets: '',
        assetsShare: '',
        valueUnit: '',
        valueCombined: '',
        virtualResults: '',
        valueVirtual: '',
      },
    }
  },
  methods: {
    init(row) {
      this.visible = true
      this.title = '添加邮件模板'
      this.$nextTick(() => {
        this.dataFrom = {
          id: '',
          organName: '',
          customerName: '',
          productName: '',
          valueDate: '',
          valueAssets: '',
          assetsShare: '',
          valueUnit: '',
          valueCombined: '',
          virtualResults: '',
          valueVirtual: '',
        }
        if (row) {
          this.title = '修改邮件模板'
          this.dataFrom.id = row.id
          this.findInvestOrganByOrganId()
        }
      })
    },
    findInvestOrganByOrganId() {
      findInvestOrganByOrganId({ id: this.dataFrom.id }).then((res) => {
        this.dataFrom.organName = res.body.organName
        const positionArr = JSON.parse(res.body.position)
        positionArr.forEach((element) => {
          for (const key in element) {
            if (Object.hasOwnProperty.call(element, key)) {
              switch (key) {
                case '客户名称':
                  this.dataFrom.customerName = element[key]
                  break
                case '产品名称':
                  this.dataFrom.productName = element[key]
                  break
                case '净值日期':
                  this.dataFrom.valueDate = element[key]
                  break
                case '资产净值':
                  this.dataFrom.valueAssets = element[key]
                  break
                case '资产份额':
                  this.dataFrom.assetsShare = element[key]
                  break
                case '单位净值':
                  this.dataFrom.valueUnit = element[key]
                  break
                case '累计净值':
                  this.dataFrom.valueCombined = element[key]
                  break
                case '虚拟业绩报酬':
                  this.dataFrom.virtualResults = element[key]
                  break
                case '虚拟净值':
                  this.dataFrom.valueVirtual = element[key]
                  break
                default:
                  break
              }
            }
          }
        })
        console.log(this.dataFrom)
      })
    },
    addSubmit() {
      const data = {
        id: this.dataFrom.id,
        organName: this.dataFrom.organName,
        position: JSON.stringify([
          { 客户名称: this.dataFrom.customerName },
          { 产品名称: this.dataFrom.productName },
          { 净值日期: this.dataFrom.valueDate },
          { 资产净值: this.dataFrom.valueAssets },
          { 资产份额: this.dataFrom.assetsShare },
          { 单位净值: this.dataFrom.valueUnit },
          { 累计净值: this.dataFrom.valueCombined },
          { 虚拟业绩报酬: this.dataFrom.virtualResults },
          { 虚拟净值: this.dataFrom.valueVirtual },
        ]),
      }
      addOrUpdateOrgan(data).then((res) => {
        this.$notify({
          message: this.dataFrom.id ? '修改成功' : '新增成功',
          type: 'success',
          duration: 2000,
        })
        this.$emit('openClose')
        this.visible = false
      })
    },
  },
}
</script>

<style lang="scss" scoped></style>
